/**
 * Description: 给定字符串数组，使用Map的key来保存数组中字符串元素，value保存该字符串元素的出现次数，最后统计出个字符串元素的出现次数
 * Class Name:subject3
 * Date:2022/3/14 18:24
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第8章_Java集合.课后练习;

import java.util.*;

public class subject3 {
    public static void main(String[] args) {
        String[] ss = {"a", "b", "a", "b", "c", "a", "b", "c", "b"};
        System.out.println("数组为：" + Arrays.toString(ss));
        // 最终存放map的对象
        var hashmap = new HashMap<>();
        // 辅助用集合，用来保存不重复的元素
        var hashset = new HashSet<>();
        // 辅助用线性表，用来保存出现
        var list = new ArrayList<>();

        /*
        先将数组元素放入到Set中和List中
         */
        Collections.addAll(hashset, ss);
        Collections.addAll(list, ss);

        /*
        下面开始使用list和hashmap来实现字典
         */
        for (var member :
                hashset) {
            int frequency = Collections.frequency(list, member);
            hashmap.put(member, frequency);
        }

        System.out.println(hashmap);
    }
}
